JMap = function(yMap) {
    this.points = {};
    this.map = yMap;
    this.overlayStyles = {};
    this.overlays = {};
    this.myLocation = null;

    YMaps.Events.observe(this.map, this.map.Events.Click, function(map, event) {
        if ('Client' in window) {
            var pt = event.getGeoPoint();
            Client.notifyMapClick(pt.getLat(), pt.getLng());
        }
    });
};

JMap.prototype.showOverlayAt = function(name, style, lat, lon) {
    if (style in this.overlayStyles) {
        var where = new YMaps.GeoPoint(lon / 1e6, lat / 1e6);
        if (name in this.overlays) {
            this.overlays[name].setGeoPoint(where);
        } else {
            this.map.addOverlay(
                    this.overlays[name] = new YMaps.Placemark(where, {
                        style: this.overlayStyles[style],
                        hasBalloon: false,
                        hasHint: false}));
        }
    }
};

JMap.prototype.showPointOverlay = function(pointId, style, lat, lon, radius) {
    if (style in this.overlayStyles) {
        var point;
        var where = new YMaps.GeoPoint(lon / 1e6, lat / 1e6);
        if (pointId in this.points) {
            point = this.points[pointId];
            point.setCenter(where);
            if(radius)
                point.setRadius(radius);
        }
        else
            this.points[pointId] = new HippoPointOverlay(this.map, where, radius, this.overlayStyles[style])
    }
};

JMap.prototype.showMyLocation = function(style, lat, lon, radius) {
    if (style in this.overlayStyles) {
        var where = new YMaps.GeoPoint(lon / 1e6, lat / 1e6);
        if (this.myLocation !== null) {
            this.myLocation.setCenter(where);
            if(radius)
                this.myLocation.setRadius(radius > 30 ? radius : 0);
        }
        else
            this.myLocation = new HippoPointOverlay(this.map, where, radius > 30 ? radius : 0, this.overlayStyles[style])
    }
};

JMap.prototype.hideMyLocation = function() {
    if(this.myLocation !== null) {
        this.myLocation.remove();
        this.myLocation = null;
    }
};

JMap.prototype.hideOverlay = function(name) {
    if (name in this.overlays) {
        this.map.removeOverlay(this.overlays[name]);
        delete this.overlays[name];
    }
};

JMap.prototype.registerOverlay = function(name, image, width, height, offsetx, offsety) {
    var s = new YMaps.Style();
    s.iconStyle = new YMaps.IconStyle();
    s.iconStyle.href = image;
    s.iconStyle.size = new YMaps.Point(width, height);
    s.iconStyle.offset = new YMaps.Point(offsetx, offsety);
    this.overlayStyles[name] = s;
};

JMap.prototype.setCenter = function(lat, lon) {
    this.map.setCenter(new YMaps.GeoPoint(lon / 1e6, lat / 1e6));
};

JMap.prototype.setMode = function(isSat) {
    this.map.setType(isSat ? YMaps.MapType.SATELLITE : YMaps.MapType.MAP);
};

JMap.prototype.setZoom = function(zoom) {
    this.map.setZoom(zoom, {
        smooth: true,
        position: this.map.getCenter(),
        centering: true
    });
};

JMap.prototype.zoomBy = function(step) {
    this.map.zoomBy(step, {
        smooth: true,
        position: this.map.getCenter(),
        centering: true
    });
};

JMap.prototype.recalcOrientation = function() {
    YMaps.jQuery("#map").css({
        width: window.innerWidth + 'px',
        height: window.innerHeight + 'px'
    });
};

JMap.prototype.clearPoints = function(){
    for(var i in this.points) {
        this.points[i].remove();
        delete this.points[i];
    }
};
